package top.lixunda.ecommerce.server.user.feign;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.user.api.feign.IFeignTestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试Feign调用接口
 *
 * @author lixunda
 * @version time:2020/4/21 9:12
 */
@Slf4j
@RestController
public class FeignTestController extends BaseController implements IFeignTestController {

    private final HttpSession httpSession;

    private final HttpServletRequest httpServletRequest;

    @Autowired
    public FeignTestController(HttpSession httpSession,
                               HttpServletRequest httpServletRequest) {
        this.httpSession = httpSession;
        this.httpServletRequest = httpServletRequest;
    }

    /**
     * 测试springSession同步
     *
     * @return 返回结果
     */
    @Override
    public MessageObject<Map<String, String>> testSpringSession() {
        log.info("good:  sessionId1: {}  sessionId2: {}", httpSession.getId(), getHttpSessionProxy().getId());
        Map<String, String> map = new HashMap<>(16);
        map.put("userSession1", httpSession.getId());
        map.put("userSession2", getHttpSessionProxy().getId());

        Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
        if (headerNames != null) {
            String name, value;
            while (headerNames.hasMoreElements()) {
                name = headerNames.nextElement();
                value = httpServletRequest.getHeader(name);
                log.debug("header:    name = {}  value = {}", name, value);
                map.put("userHeader:" + name, value);
            }
        }

        return getSuccessMessageObject("成功", map);
    }
}
